// packageA/pages/attendanceRules/attendanceRules.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //列表
    list:[],
    //页码
    page: 1,
    //是否无更多
    notMore: true,
    //无数据
    showNothing:false,
    //搜索关键词
    searchValue:'',
    //项目选择框
    showProject:false,
    //项目选择数据
    projectList:[],
    //项目id
    projectId:null,
    //项目名称
    projectName:'选择部门'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取列表
    this.getList();
  },

  //获取列表
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'clockRule',
      data: {
        selectFields: ['id','ruleName','ruleTime','departId','depart'],
        sort: {
          id: -1
        },
        page: {
          page: this.data.page,
          pageSize: 10
        }
      }
    }

    //规则名称模糊查询
    if(this.data.searchValue){
      param.data.ruleName='%' +this.data.searchValue+ '%';
    }

    //查询某个部门
    try {                  
      if(this.data.projectId){
        param.data.departId = this.data.projectId;
      }
    } catch (e) {}  

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;

        result.forEach(item => {
          let times=item.ruleTime;
          for(let i=0;i<times.length;i++){
            times[i].beforeTimes=util.secondToFormat(times[i].beforeTime/1000);
            times[i].afterTimes=util.secondToFormat(times[i].afterTime/1000);
          }
        });  

        console.log('考勤规则列表');
        console.log(result);

        this.setData({
          list: this.data.list.concat(result)
        })
        Toast.clear();
      }else{
        this.setData({
          notMore: false
        })
        if(this.data.page==1){
          this.setData({
            showNothing: true
          })
          Toast.clear();
        }else{
          Toast.fail('没有更多了');
        }
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
      Toast.clear();
    })
  },  

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      page:1,
      notMore: true,
      showNothing: false
    })
    this.getList();
  }, 

  //跳转添加
  goToAdd(){
    wx.navigateTo({
      url: "/packageA/pages/attendanceRuleAdd/attendanceRuleAdd"
    })
  },

  //跳转编辑
  goToEdit(e){
    Dialog.confirm({
      title: '确定修改此考勤规则吗？',
      message: '为保证人员考勤数据的准确性 \n 相应的考勤组和排班也需要您手动修改',
      confirmButtonText:'确定',
      cancelButtonText:'不改了'
    }).then(() => {      
      let detail=e.currentTarget.dataset.detail;

      let paramObj={
        id: detail.id
      }

      wx.navigateTo({
        url: "/packageA/pages/attendanceRuleAdd/attendanceRuleAdd?paramObj="+JSON.stringify(paramObj)
      })  
    }).catch(() => {
    });  
  },

  //删除
  onDelete(e){
    Dialog.confirm({
      title: '确定删除此考勤规则吗？',
      message: '为保证人员考勤数据的准确性 \n 相应的考勤组和排班也需要您手动修改',
      confirmButtonText:'确定',
      cancelButtonText:'不删除了'
    }).then(() => {
      let detail=e.currentTarget.dataset.detail;

      let param={
        className: "clockRule",
        data:{
          id: detail.id
        }
      }

      CommonService.goDelete(param).then(res=>{
        if (res.statusCode == 200 && res.data.success == true){
          Toast.success('已删除');
          this.refreshListAndPage();
        }else{
          Toast.fail('操作失败');
        }
      }).catch((error) => {
        Toast.fail('操作失败');
      })
    }).catch(() => {
    }); 
  },  

  //获取搜索词
  onChangeSearch(e) {
    this.setData({
      searchValue: e.detail.value,
    })
  },

  //点击搜索
  onSearch(){
    this.refreshListAndPage();
  },

  //显示项目
  onShowProject(){
    this.setData({
			showProject: true
    })
    this.getProjectList();
  },

  //查询项目
  getProjectList(){
    this.setData({
      projectList: []
    })

    let param = {
      isAuth: true,
      className: 'depart',
      data: {
        searchAll: true,
        selectFields: ['id','name']
      }
    }

    //模糊查询
    if(this.data.valueSearchProject){
      param.data.name='%' + this.data.valueSearchProject + '%';
    }

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let arr=[];

        result.forEach(item => {
          arr.push({
            text: item.name,
            id: item.id
          });
        });

        this.setData({
          projectList: arr
        })
      }
    }).catch((error) => {
    })
  },

  //隐藏项目
  onCloseProject(){
    this.setData({
      showProject: false,
      valueSearchProject:''
    })
  },

  //确认项目
  onConfirmProject(e){
    this.setData({
      projectId:e. detail.value.id,
      projectName: e.detail.value.text
    })
    this.onCloseProject();
    this.refreshListAndPage();
  },

  //搜索项目
  onChangeProjectVal(e){
    this.setData({
      valueSearchProject: e.detail
    })
    this.getProjectList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.notMore){
      this.setData({
        page: this.data.page + 1
      })
      this.getList();
    }
  }
})
